package com.cbgolf.oa.views;

import android.app.Activity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.amap.api.maps.MapView;
import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseViewNew;
import com.cbgolf.oa.contract.IParkMapContract.IParkMapView;
import com.cbgolf.oa.contract.IParkMapContract.IParkMapWaiter;
import com.cbgolf.oa.entity.ParkBean;
import com.cbgolf.oa.event.EventPark;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.ErrorUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.util.ViewUtils;
import org.xutils.view.annotation.ViewInject;

/**
 * @author lq
 * @date 2018/4/27
 */

public class ParkMapViewImp extends BaseViewNew implements IParkMapView {

  //top
  @ViewInject(R.id.topBack_ll)
  private View topBack;
  @ViewInject(R.id.title_tv)
  private TextView topTitle;
  @ViewInject(R.id.topRight_iv)
  private ImageView topRightIv;
  //
  @ViewInject(R.id.a_park_present_num_tv)
  private TextView playingNumTv;
  @ViewInject(R.id.a_park_present_compare_tv)
  private TextView playingRatioTv;
  @ViewInject(R.id.a_park_no_checkin_num_tv)
  private TextView noCheckInNumTv;
  @ViewInject(R.id.a_park_no_checkin_compare_tv)
  private TextView tomorrowNumTv;
  @ViewInject(R.id.a_park_car_num_tv)
  private TextView carUsingNumTv;
  @ViewInject(R.id.a_park_car_num_compare_tv)
  private TextView carFreeNumTv;
  @ViewInject(R.id.a_park_caddie_num_tv)
  private TextView caddieWorkingTv;
  @ViewInject(R.id.a_park_caddie_compare_tv)
  private TextView caddieFreeNumTv;
  //
  @ViewInject(R.id.ll_parkmap_inpark)
  private View inParkView;
  @ViewInject(R.id.ll_parkmap_noreg)
  private View noRegView;
  @ViewInject(R.id.ll_parkmap_car)
  private View carView;
  @ViewInject(R.id.ll_parkmap_caddie)
  private View caddieView;

  //
  private MapView mapView;


  private IParkMapWaiter worker;

  public ParkMapViewImp(Activity act, IParkMapWaiter waiter, MapView mapView) {
    this.worker = waiter;
    this.mapView = mapView;
    ViewUtils.autoInjectAllField(this, act);
    super.init(act);
  }

  @Override
  protected void setViews() {
    ViewUtils.setText(topTitle, worker.getActivityTitle());
    ViewUtils.setImage(topRightIv, R.mipmap.qcsk_refresh);
    ViewUtils.setVisible(topRightIv, true);
  }

  @Override
  protected void setListener() {
    ViewUtils.setClick(topRightIv, v -> worker.refresh());
    ViewUtils.setClick(topBack, v -> worker.back());
    ViewUtils.setClick(inParkView, v -> worker.showPlayerPlaying());
    ViewUtils.setClick(noRegView, v -> worker.showPlayerOfNoReg());
    ViewUtils.setClick(caddieView, v -> worker.showCaddieOfWorking());
    ViewUtils.setClick(carView, v -> worker.showCarOfUsing());
  }

  @Override
  public void showProgress() {
    AnimaUtil.showLoading(context);
  }

  @Override
  public void stopProgress() {
    AnimaUtil.stopLoading();
  }

  @Override
  public void requestFail(String msg, int code) {
    Util.show(msg);
  }

  @Override
  public void requestOver(EventPark event) {
    stopProgress();
    ParkBean bean = event.parkBean;
    if (null == bean) {
      requestFail(ErrorUtil.NODATA, ErrorUtil.NODATA_CODE);
      return;
    }
    showData(bean);
  }

  private int noCheckInNum;
  private int tomorrowNum;
  private int carUsingNum;
  private int carFreeNum;
  private int caddieWorkingNum;
  private int caddieFreeNum;


  private void showData(ParkBean m) {
    if (null != m.recordData) {
      noCheckInNum = m.recordData.recordNum;
      tomorrowNum = m.recordData.tomorrowNum;
    }
    if (null != m.carRecordData) {
      carUsingNum = m.carRecordData.usedNum;
      carFreeNum = m.carRecordData.freeNum;
    }
    if (null != m.caddieRecordData) {
      caddieWorkingNum = m.caddieRecordData.usedNum;
      caddieFreeNum = m.caddieRecordData.freeNum;
    }
    noCheckInNumTv.setText(String.valueOf(noCheckInNum));
    tomorrowNumTv.setText("明日" + String.valueOf(tomorrowNum) + "人");
    carUsingNumTv.setText(String.valueOf(carUsingNum));
    carFreeNumTv.setText("空闲" + String.valueOf(carFreeNum) + "辆");
    caddieWorkingTv.setText(String.valueOf(caddieWorkingNum));
    caddieFreeNumTv.setText("空闲" + String.valueOf(caddieFreeNum) + "人");
  }

  @Override
  public void showPlayerNumInPark(int num) {
    ViewUtils.setText(playingNumTv, num + "");
  }

  @Override
  public void showParkWayRatio(EventPark event) {
    if (null == event) {
          return;
    }
    ViewUtils.setText(playingRatioTv, "占用率" + (Util.isNull(event.comePare) ? "0" : event.comePare));
  }
}
